import setting from '@/settings.js'
import QQMapWX from "@/utils/qqmap-wx-jssdk.min.js";
export default {
	namespaced:true,
	state:{
		//客户来源
		memberSource : 1,
		//邀请人id
		inviterId : '',
		//是否登录
		isLogin : false,
		userInfo:{
		},
		//认证状态 1:未认证 2已认证
		censorStatus : 1,
		token:'',
		openid:'',
		tenantId: setting.tenantId,
		
		//定位城市信息
		lat: 0,
		lng : 0,
		locationCity:'',
		locationAddress:'',
		locationCityCode:'',
		
		/* 短租 */
		//取车城市信息
		city:'',
		address : '',
		cityCode : '',
		takeStoreId:'',
		takeCarLat:0,
		takeCarLng:0,
		//取车门店营业时间
		takeStoreBusinessStartTime:'00:00',
		takeStoreBusinessEndTime:'23:30',


		//还车城市信息
		returnCity:'',
		returnAddress : '',
		returnCityCode : '',
        returnStoreId:'',
		returnCarLat:0,
		returnCarLng:0,
		//还车门店营业时间
		returnStoreBusinessStartTime:'00:00',
		returnStoreBusinessEndTime:'23:30',
		 
		/* 长租 */
		lCityId : '',
		lCityName : '',
		
	   
		
	},
	mutations:{
		saveMemberSource(state, memberSource) {
		 state.memberSource = memberSource;	
		},
		saveinviterId(state, inviterId) {
			state.inviterId = inviterId;
		},
		saveIsLogin(state, isLogin) {
		  state.isLogin = isLogin;
		},
		//用户信息
		saveUserInfo(state, userinfo){
			state.userInfo = userinfo;
		},
		saveCensorStatus(state, censorStatus) {
			state.censorStatus = censorStatus;
		},
		saveToken(state, token){
			state.token = token
		},
		saveOpenId(state, openId){
			state.openid = openId
		},
	
		//定位信息
		saveLat(state, lat) {
			state.lat = lat;
		},
		saveLng(state, lng) {
			state.lng = lng;
		},
		saveLocationCity(state,locationCity) {
			state.locationCity =  locationCity;
		},
		saveLocationAddress(state, locationAddress) {
			state.locationAddress = locationAddress;
		},
		saveLocationCityCode(state, locationCityCode) {
		  	state.locationCityCode = locationCityCode;
		},
		
		//取车信息
		saveCity(state, city) {
			state.city = city;
		},
		saveAddress(state, address) {
			state.address = address;
		},
		saveCityCode(state, cityCode) {
			state.cityCode = cityCode;
		},
		saveTakeStoreId(state, takeStoreId) {
		    state.takeStoreId = takeStoreId;	
		},
		saveTakeCarLat(state,takeCarLat) {
			state.takeCarLat = takeCarLat;
		},
		saveTakeCarLng(state,takeCarLng) {
			state.takeCarLng = takeCarLng;
		},
		saveTakeStoreBusinessStartTime(state, businessStartTime){
			state.takeStoreBusinessStartTime = businessStartTime;
		},
		saveTakeStoreBusinessEndTime(state, businessEndTime){
			state.takeStoreBusinessEndTime = businessEndTime;
		},

		
		
		//还车信息
		saveReturnCity(state, returnCity) {
			state.returnCity = returnCity;
		},
		saveReturnAddress(state, returnAddress) {
			state.returnAddress = returnAddress;
		},
		saveReturnCityCode(state, returnCityCode) {
			state.returnCityCode = returnCityCode;
		},
		saveReturnStoreId(state, returnStoreId) {
			state.returnStoreId = returnStoreId;
		},
		saveReturnCarLat(state, returnCarLat) {
			state.returnCarLat = returnCarLat;
		},
		saveReturnCarLng(state, returnCarLng) {
			state.returnCarLng = returnCarLng;
		},
		saveReturnStoreBusinessStartTime(state, businessStartTime){
			state.returnStoreBusinessStartTime = businessStartTime;
		},
		saveReturnStoreBusinessEndTime(state, businessEndTime){
			state.returnStoreBusinessEndTime = businessEndTime;
		},
		
		/* 长租 */
		//城市id
		saveLCityId(state, lCityId) {
			state.lCityId = lCityId
		},
		savelCityName(state, lCityName) {
			state.lCityName = lCityName;
		}
		
	},
	
	 actions: {
		getCity(context) {
			// 向用户发起授权请求，弹框提示
			uni.authorize({
				// 获取用户定位信息
				scope:"scope.userLocation",
				// 用户同意授权执行
				success() {
		
					// 引入腾讯地图api
				    // 实例化API核心类
				    let qqmapsdk = new QQMapWX({
				         // 填写自己的Key值，这个值是与AppID绑定的
				         key: setting.mapKey
				     });
				    //获取位置信息
					uni.getLocation({
						type:'gcj02',
						success: (res) => {
							context.commit('saveLat', res.latitude);
							context.commit('saveLng', res.longitude);
							context.commit('saveTakeCarLat', res.latitude);
							context.commit('saveTakeCarLng', res.longitude);
							context.commit('saveReturnCarLat', res.latitude);
							context.commit('saveReturnCarLng', res.longitude);
							uni.$emit('locationSuccess');
							//逆地理解析方法
							qqmapsdk.reverseGeocoder({
								location:{
									latitude : res.latitude,
									longitude : res.longitude
								},
								success:(res)=>{
									console.log(res);
									// 取到用户的定位城市，赋值传递出去
									//当前城市
									let city = res.result.ad_info.city;
									//当前城市编码
									var city_code = res.result.ad_info.city_code;
									city_code = city_code.slice(3);
									//具体定位位置
									let address = res.result.formatted_addresses.recommend;
									
									console.log(city,city_code, address);
									//全局保存城市定位信息
									
									//定位信息
									context.commit('saveLocationCity', city);
									context.commit('saveLocationCityCode', city_code);
									context.commit('saveLocationAddress', address);
									
									//取车地址信息->默认定位信息
									context.commit('saveCity', city);
									context.commit('saveCityCode', city_code);
									context.commit('saveAddress', address);
									
									//还车地址信息->默认定位信息
									context.commit('saveReturnCity', city);
									context.commit('saveReturnCityCode', city_code);
									context.commit('saveReturnAddress', address);
									
									//长租取车城市
									context.commit('savelCityName', city);
									context.commit('saveLCityId', city_code);
									
								}
							})
						}
					})
					
				},
				
				fail: (res) => {
					uni.showModal({
						title: '温馨提示',
						content: '行知出行需要获取您的定位信息,否则部分功能将无法使用,请前往设置页面打开定位权限',
						showCancel: false,
						confirmText: '设置',
						success: (res) => {
							if(res.confirm) {
								uni.openSetting()
							}
						}
						
					})
				}
				
			})
		}
	}
}
